<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserSessionImpl
 *
 * @author Administrator
 */

namespace frontend\dao\impl;

use frontend\bean\UserSession;
use frontend\dao\UserSessionDao;
use Yii;

class UserSessionDaoImpl implements UserSessionDao {

    //put your code here

    public function getSessionBySid($sid) {
        $connection = Yii::$app->db;
        $sql = "SELECT * FROM `user_session` WHERE sid=:sid ";
        $command = $connection->createCommand($sql);
        $command->bindValue(':sid', $sid);
        $result = $command->queryOne();
        return $result ? (new UserSession())->load($result) : null;
    }

    public function delSession(UserSession $bean) {
        $connection = Yii::$app->db;
        $sql = "DELETE  FROM  `user_session`  where sid=:sid";
        $command = $connection->createCommand($sql);
        $command->bindValue(':sid', $bean->getSid());
        return $command->execute();
    }

    public function addSession(UserSession $bean) {

        $connection = Yii::$app->db;
        $sql = "INSERT INTO  `user_session`  (sid,uid,expire,dateline,actionline) VALUES (:sid,:uid,:expire,:dateline,:actionline)";
        $command = $connection->createCommand($sql);
        $command->bindValue(':sid', $bean->getSid());
        $command->bindValue(':uid', $bean->getUid());
        $command->bindValue(':expire', $bean->getExpire());
        $command->bindValue(':dateline', $bean->getDateline());
        $command->bindValue(':actionline', $bean->getActionline());
        return $result = $command->execute();
    }

    public function updateSession(MongoUserSession $session) {

        return $this->_getSessionDao()->updateSession($session);
    }

}
